package com.chinaunicom.product.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.chinaunicom.common.constants.Constants;
import com.chinaunicom.product.domain.model.entity.ProductSkuEntity;
import com.chinaunicom.product.domain.model.query.ProductSpuQuery;
import com.chinaunicom.product.domain.repository.IProductRepository;
import com.chinaunicom.product.infrastructure.database.mapper.ProductSkuMapper;
import com.chinaunicom.product.infrastructure.database.po.ProductSkuPO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author guomenghuai
 * @date 2024/1/10
 * @Description
 */
@Repository
public class ProductRepository implements IProductRepository {

    @Resource
    private ProductSkuMapper productMapper;

    @Override
    public List<ProductSkuEntity> queryProductList(ProductSpuQuery query) {
        LambdaQueryWrapper<ProductSkuPO> queryWrapper = buildQueryWarpper(query);
        queryWrapper.orderByDesc(ProductSkuPO::getCreateTime);
        List<ProductSkuPO> productPOList = productMapper.selectList(queryWrapper);
        List<ProductSkuEntity> productEntityList = productPOList.stream().map(item -> {
            ProductSkuEntity productEntity = new ProductSkuEntity();
            BeanUtils.copyProperties(item, productEntity);
            return productEntity;
        }).collect(Collectors.toList());
        return productEntityList;
    }

    private static LambdaQueryWrapper<ProductSkuPO> buildQueryWarpper(ProductSpuQuery query) {
        LambdaQueryWrapper<ProductSkuPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(query.getProductId()), ProductSkuPO::getId, query.getProductId());
        queryWrapper.eq(Objects.nonNull(query.getStatus()), ProductSkuPO::getStatus, query.getStatus());
        queryWrapper.eq(ProductSkuPO::getDeleted, Constants.NOT_DELETED);
        return queryWrapper;
    }

    @Override
    public ProductSkuEntity queryProductById(ProductSpuQuery query) {
        LambdaQueryWrapper<ProductSkuPO> queryWrapper = buildQueryWarpper(query);
        List<ProductSkuPO> productPOList = productMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(productPOList)) {
            return new ProductSkuEntity();
        }

        ProductSkuEntity productEntity = new ProductSkuEntity();
        ProductSkuPO productPO = productPOList.get(0);
        BeanUtils.copyProperties(productPO, productEntity);
        return productEntity;
    }

    @Override
    public List<ProductSkuEntity> queryListProductSkuByCode(List<String> skuCodeList, String sellerId) {
        LambdaQueryWrapper<ProductSkuPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ProductSkuPO::getSkuCode, skuCodeList);
        queryWrapper.eq(StringUtils.isNotBlank(sellerId), ProductSkuPO::getSellerId, sellerId);

        List<ProductSkuPO> productSkuPOList = productMapper.selectList(queryWrapper);
        List<ProductSkuEntity> productSkuEntityList = productSkuPOList.stream().map(item -> {
            ProductSkuEntity productEntity = new ProductSkuEntity();
            BeanUtils.copyProperties(item, productEntity);
            productEntity.setProductId(item.getId());
            return productEntity;
        }).collect(Collectors.toList());
        return productSkuEntityList;
    }

    @Override
    public void saveProduct(ProductSkuEntity productSkuEntity) {
        ProductSkuPO productSkuPO = new ProductSkuPO();
        BeanUtils.copyProperties(productSkuEntity, productSkuPO);
        productSkuPO.setId(IdWorker.getId());
        productSkuPO.setSkuCode(String.valueOf(IdWorker.getId()));
        productSkuPO.setProductCode(String.valueOf(productSkuPO.getId()));
        productMapper.insert(productSkuPO);
    }
}
